<?php
/**
 * Module Paths Controller Plugin
 * Because the request object is not available within the bootstrap (due to
 * bootstrapping happening before routing), paths that need to know which module
 * we are currently in cannot be placed in the bootstrap and are instead placed
 * within this plugin.
 *
 * @package     Visi
 * @subpackage  Controller Plugins
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2010 Luke Visinoni <luke.visinoni@gmail.com>
 * @version     $Id$
 * @todo        Make sure this goes in the Visi library and not in the ext
 */
class Visi_Controller_Plugin_ModulePaths extends Zend_Controller_Plugin_Abstract {

    protected $_view;
    
    public function __construct(Zend_View_Abstract $view) {
    
        $this->setView($view);
    
    }
    
    public function setView(Zend_View_Abstract $view) {
    
        $this->_view = $view;
        return $this;
    
    }
    
    public function postDispatch(Zend_Controller_Request_Abstract $request) {
    
        $front = Zend_Controller_Front::getInstance();
        $module = $request->getModuleName();
        $defaultModule = $front->getDefaultModule();
        // Define the base view path
        // @todo This doesn't work :(
        // $this->_view->addBasePath(APPLICATION_PATH . '/modules/' . $module . '/views', 'Visi_View_');
        // First add a helper path to the current module's helpers
        $this->_view->addHelperPath($front->getModuleDirectory() . '/views/helpers/', 'Visi_View_Helper_');
        // If in a module other than the default, also add the default
        if ($module != $defaultModule) {
            // Not sure why, but "getModuleDirectory()" is returning the module path plus the current module directory
            $modulePath = realpath(Zend_Controller_Front::getInstance()->getModuleDirectory(). '/../');
            $helperPath = $modulePath . '/' . $defaultModule . '/views/helpers/';
            $this->_view->addHelperPath($helperPath, 'Visi_View_Helper_');
        }
    
    }

}